home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0038_Loading Images from Disk.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  3KB  |  151 lines

  1. {
  2. STEFAN XENOS
  3.  
  4. > I am able to load an image into a buffer and display it with PutImage ect.,
  5. > but I would like to load the image from disk instead of with getimage.
  6.  
  7. Name: ImageStuff.Pas
  8. Purpose: ImageStuff is a unit for storing bitmaps in dynamic variables and
  9.          writing them to disk.
  10. Progger: Stefan Xenos
  11.  
  12. This unit is public domain.}
  13.  
  14. Unit ImageStuff;
  15.  
  16. interface
  17.  
  18. Uses
  19.  Graph;
  20.  
  21. Type
  22.   Image = Record
  23.     BitMap : Pointer;
  24.     Size   : Word;
  25.  end;
  26.  
  27. Procedure Get(X1, Y1, X2, Y2 : Word; Var aImage : Image);
  28. Procedure Put(X, Y : Word; aImage : Image; BitBlt : Word);
  29. Procedure Kill(Var aImage : Image);
  30. Procedure Save(Var F : File; aImage : Image);
  31. Procedure Load(Var F : File; Var aImage : Image);
  32.  
  33. implementation
  34.  
  35. Procedure Get(X1, Y1, X2, Y2 : Word; Var aImage : Image);
  36. {Clips an image from the screen and store it in a dynamic variable}
  37. Begin
  38.   aImage.bitmap := nil;
  39.   aImage.size   := ImageSize(X1, Y1, X2, Y2);
  40.   GetMem(aImage.BitMap,aImage.Size);    {Ask for some memory}
  41.   GetImage(X1, Y1, X2, Y2, aImage.BitMap^); {Copy the image}
  42. End;
  43.  
  44. Procedure Put(X, Y : Word; aImage : Image; BitBlt : Word);
  45. Begin
  46.   PutImage(X, Y, aImage.BitMap^, BitBlt);   {Display image}
  47. End;
  48.  
  49. Procedure Kill(Var aImage : Image);
  50. {Frees up the memory used by an unwanted image}
  51. Begin
  52.   FreeMem (aImage.BitMap, aImage.Size); {Free up memory used by image}
  53.   aImage.Size   := 0;
  54.   aImage.BitMap := Nil;
  55. End;
  56.  
  57. Procedure Save(Var F : File; aImage : Image);
  58. {Saves an image to disk. File MUST already be opened for write}
  59. Begin
  60.   BlockWrite(F, aImage.Size, 2);             {Store the image's size so that
  61.                                             it may be correctly loaded later}
  62.   BlockWrite(F, aImage.BitMap^, aImage.Size); {Write image itself to disk}
  63. End;
  64.  
  65. Procedure Load (Var F : File; Var aImage : Image);
  66. {Loads an image off disk and stores it in a dynamic variable}
  67. Begin
  68.  BlockRead(F, aImage.Size, 2);              {Find out how big the image is}
  69.  GetMem(aImage.BitMap, aImage.Size);        {Allocate memory for it}
  70.  BlockRead(F, aImage.BitMap^, aImage.Size)  {Load the image}
  71. End;
  72.  
  73. Begin
  74. End.
  75.  
  76. {
  77. Here's some source which should help you figure out how to use the unit I
  78. just sent.
  79. }
  80.  
  81. {By Stefan Xenos}
  82. Program ImageTest;
  83.  
  84. Uses
  85.   Graph,
  86.   ImageStuff;
  87.  
  88. Var
  89.   Pic      : Image;
  90.   LineNum  : Byte;
  91.   DataFile : File;
  92.   GrDriver,
  93.   GrMode   : Integer;
  94.  
  95. Const
  96.  FileName = 'IMAGE.DAT';
  97.  MaxLines = 200;
  98.  
  99. Begin
  100.  {Initialise}
  101.  DetectGraph(GrDriver, GrMode);
  102.  InitGraph(GrDriver, GrMode, '');
  103.  Randomize;
  104.  
  105.  {Draw some lines}
  106.  For LineNum := 1 to MaxLines do
  107.  begin
  108.    setColor(random (maxcolors));
  109.    line(random(getmaxx), random(getmaxy), random(getmaxx), random(getmaxy));
  110.  end;
  111.  
  112.  {Copy image from screen}
  113.  Get(100, 100, 150, 150, Pic);
  114.  
  115.  readLn;
  116.  
  117.  {Clear screen}
  118.  ClearDevice;
  119.  
  120.  {Display image}
  121.  Put(100, 100, Pic, NormalPut);
  122.  
  123.  readLn;
  124.  
  125.  {Clear screen}
  126.  ClearDevice;
  127.  
  128.  {Save image to disk}
  129.  Assign(DataFile, FileName);
  130.  Rewrite(DataFile, 1);
  131.  Save(DataFile, Pic);
  132.  Close(DataFile);
  133.  
  134.  {Kill image}
  135.  Kill(pic);
  136.  
  137.  {Load image from disk}
  138.  Assign(DataFile, FileName);
  139.  Reset(DataFile, 1);
  140.  Load(DataFile, pic);
  141.  Close(DataFile);
  142.  
  143.  {Display image}
  144.  Put(200, 200, Pic, NormalPut);
  145.  
  146.  readLn;
  147.  
  148.  CloseGraph;
  149.  WriteLn(Pic.size);
  150. End.
  151.